﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Organizer.pages
{
    public partial class Movies : System.Web.UI.Page
    {
        #region Properties
        private BLOrganizer.BLMovies BLMovies;
        private int MovieId { get { return Int32.Parse(hfMovieId.Value.ToString()); } set { hfMovieId.Value = value.ToString(); } }
        private int UserId { get { return Int32.Parse(Page.RouteData.Values["UserId"].ToString()); } }
        #endregion

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                LoadMovies();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void grdMovies_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                if (BLMovies == null) { BLMovies = new BLOrganizer.BLMovies(); }

                switch (e.CommandName.ToString())
                {
                    case "Editar":
                        DataTable Movie = BLMovies.GetMovieById(Int32.Parse(e.CommandArgument.ToString()));
                        txtName.Text = Movie.Rows[0]["Name"].ToString();
                        txtType.Text = Movie.Rows[0]["Type"].ToString();
                        txtViewedDate.Text = Movie.Rows[0]["ViewedDate"].ToString();
                        txtScore.Text = Movie.Rows[0]["Score"].ToString();
                        MovieId = Int32.Parse(Movie.Rows[0]["MovieId"].ToString());
                        break;

                    case "Apagar":
                        BLMovies.DeleteMovie(Int32.Parse(e.CommandArgument.ToString()));
                        FeedBackMsg("Movie deleted successfully.", (int)controls.AlertTypes.Sucess);
                        break;

                    default:
                        break;
                }
                LoadMovies();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void grdMovies_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grdMovies.PageIndex = e.NewPageIndex;
                LoadMovies();
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (BLMovies == null) { BLMovies = new BLOrganizer.BLMovies(); }

                if (MovieId.Equals(0))
                {
                    BLMovies.AddMovie(UserId, txtName.Text, txtType.Text, txtViewedDate.Text, txtScore.Text);
                    FeedBackMsg("Movie added successfully.", (int)controls.AlertTypes.Sucess);
                }
                else
                {
                    BLMovies.UpdateMovie(MovieId, txtName.Text, txtType.Text, txtViewedDate.Text, txtScore.Text);
                    FeedBackMsg("Movie updated successfully.", (int)controls.AlertTypes.Sucess);
                }
                LoadMovies();
                ClearFields();
                MovieId = 0;
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                ClearFields();
                MovieId = 0;
            }
            catch (Exception ex)
            {
                FeedBackMsg(ex.Message.ToString(), (int)controls.AlertTypes.Error);
            }
        }
        #endregion

        #region Methods
        private void ClearFields()
        {
            try
            {
                txtName.Text = string.Empty;
                txtType.Text = string.Empty;
                txtViewedDate.Text = string.Empty;
                txtScore.Text = string.Empty;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void LoadMovies()
        {
            try
            {
                if (BLMovies == null) { BLMovies = new BLOrganizer.BLMovies(); }
                grdMovies.DataSource = BLMovies.GetMoviesByUser((int)UserId);
                grdMovies.DataBind();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        private void FeedBackMsg(string Text, int AlertType)
        {
            try
            {
                Main Mn = (Main)Page.Master;
                Mn.FeedBackMsg(Text, AlertType);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}